﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;

namespace Code.UI
{
    public class SkillItem:MonoBehaviour,IPointerClickHandler,IPointerEnterHandler,IPointerExitHandler
    {
        public GameObject _skillDescUIPrefab;
        public Image _icon;
        public Image _mask;

        private SkillDescUI _skillDescUI;
        private UIPlayer _currentPlayer;
        private SkillData _data;
        
        public void InitSkillItem(UIPlayer player,SkillData skillData) 
        {
            _data = skillData;
            _currentPlayer = player;
            _icon.sprite = skillData.Config.Icon;
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            Debug.Log("鼠标点击");
            if (!_currentPlayer.CanUseSkill(_data)) { return; }
            _currentPlayer.UseSkill(_data);
            _mask.fillAmount = 1;
            
            float duration=_data.Config.CoolDownTime;
            DOTween.To(() => duration, value => _mask.fillAmount = value / _data.Config.CoolDownTime, 0, _data.Config.CoolDownTime);
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            Debug.Log("鼠标Enter");
            if (_skillDescUI == null)
            {
                var obj = GameObject.Instantiate(_skillDescUIPrefab, transform.position, Quaternion.identity);
                obj.transform.SetParent(GameUI.Instance.transform);
                _skillDescUI = obj.GetComponent<SkillDescUI>();
                _skillDescUI.SetData(_data);
            }
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            Debug.Log("鼠标Exit");
            if (_skillDescUI != null) 
            {
                Destroy(_skillDescUI.gameObject);
            }
        }

    }
}
